<?php

namespace app\admin\controller;
use app\admin\model\UserModel;
use app\admin\model\UserType;
use think\Db;
use think\Session;

class User extends Base
{

    /**
     * [index 用户列表]
     * @return [type] [description]
     * @author
     */
    public function index(){
        if(request()->isAjax ()){
            extract(input());
            $map = [];
//            if(isset($role)&&$role != ""){
//                $map['ag.id'] = $role;
//            }
            //管理员账号模糊查询
            if(isset($key)&&$key!="")
            {
                $map['a.username'] = ['like',"%" . $key . "%"];
            }
//            if(isset($start)&&$start!=""&&isset($end)&&$end=="")
//            {
//                $map['a.last_login_time'] = ['>= time',$start];
//            }
//            if(isset($end)&&$end!=""&&isset($start)&&$start=="")
//            {
//                $map['a.last_login_time'] = ['<= time',$end];
//            }
//            if(isset($start)&&$start!=""&&isset($end)&&$end!="")
//            {
//                $map['a.last_login_time'] = ['between time',[$start,$end]];
//            }
            $Nowpage = input('page') ? input('page'):1;
            $limits = input("limit")?input("limit"):10;// 获取总条数;
            $field=input('field');//字段
            $order=input('order');//排序方式
            if($field && $order){
                $od="a.".$field." ".$order;
            }else{
                $od="a.create_time desc";
            }
            $count = Db::name('admin')
                ->alias('a')
                ->join('auth_group ag', 'a.groupid = ag.id','left')
                ->where($map)
                ->count();//计算总页面
            $user = new UserModel();
            $lists = $user->getUsersByWhere($map,$od, $Nowpage, $limits);
            foreach($lists as $k=>$v)
            {
                if(!empty($lists[$k]['last_login_time'])){
                    $lists[$k]['last_login_time'] = date('Y-m-d H:i',$v['last_login_time']);
                }else{
                    $lists[$k]['last_login_time'] = "";
                }
                
            }
            return json(['code'=>0,'msg'=>'','count'=>$count,'data'=>$lists]);
        }
        $role = Db::name('auth_group')->field('id,title')->order('create_time desc')->select();
        $this->assign ('role',$role);
        return $this->fetch("user/index");
    }


    /**
     * [userAdd 添加用户]
     * @return [type] [description]
     * @author
     */
    public function userAdd()
    {
        if(request()->isAjax()){
            extract(input());
            $param = input('post.');
            if(!isset($status)){
                $param['status'] = 2;
            }
            $user = new UserModel();
            $param['password'] = md5(md5($param['password']) . config('auth_key'));
            $base64url = $param['portrait'];
            //匹配出图片的格式
            $base64url = str_replace(' ', '+', $base64url);
            if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64url, $result)){
                $type = $result[2];
                $new_file = ROOT_PATH . 'public' . DS . 'uploads/face/'.date('Ymd',time())."/";
                if(!file_exists($new_file)){
                    //检查是否有该文件夹，如果没有就创建，并给予最高权限
                    mkdir($new_file, 0700);
                }
                $new_file = $new_file.time().".{$type}";
                if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64url)))){
                    $new = md5(time());
                    //压缩图片
                    image_png_size_add($new_file,ROOT_PATH . 'public' . DS . 'uploads/face/'.date('Ymd',time())."/".$new.".{$type}");
                    $url =  "http://".$_SERVER['HTTP_HOST'];
                    $file_name = $url."/uploads/face/".date('Ymd',time())."/".$new.".{$type}";
                    $param['portrait'] = $file_name;
                    $flag = $user->insertUser($param);
                    //删除未压缩前图片
                    unlink($new_file);
                    $accdata = array(
                        'uid'=> $user['id'],
                        'group_id'=> $param['groupid'],
                    );
                    $group_access = Db::name('auth_group_access')->insert($accdata);
                    return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
                }else{
                    writelog(session('uid'),session('username'),'添加管理员【'.$param['username'].'】上传头像失败',2);
                    return json(['code'=>100,'msg'=>"上传失败"]);
                }
            }else{
                $flag = $user->insertUser($param);
                $accdata = array(
                    'uid'=> $user['id'],
                    'group_id'=> $param['groupid'],
                );
                $group_access = Db::name('auth_group_access')->insert($accdata);
                return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
            }
        }else{
            $role = new UserType();
            $this->assign('role',$role->getRole());
            return $this->fetch('user/useradd');
        }
    }

    /**
     * checkName 验证管理员名称唯一性
     */
    public function checkName(){
        extract(input());
        if(isset($id)&&$id!=""){
            $uid = $id;
        }else{
            $uid = '';
        }
        $user = new UserModel();
        echo $user->checkName ($username,$uid);
    }
    
    /**
     * checkPhone 验证手机号的合法性
     */
    public function checkPhone(){
        $params = input('param.');
        if(!preg_match("/^1[345678]{1}\d{9}$/",$params['phone'])){
            echo "false";
        }else{
            echo "true";
        }
    }

    /**
     * [userEdit 编辑用户]
     * @return [type] [description]
     * @author
     */
    public function userEdit()
    {
        $user = new UserModel();
        if(request()->isAjax()){
            $param = input('post.');
            if(empty($param['password'])){
                unset($param['password']);
            }else{
                $param['password'] = md5(md5($param['password']) . config('auth_key'));
            }
            $base64url = $param['portrait'];
            //匹配出图片的格式
            $base64url = str_replace(' ', '+', $base64url);
            if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64url, $result)){
                $type = $result[2];
                $new_file = ROOT_PATH . 'public' . DS . 'uploads/face/'.date('Ymd',time())."/";
                if(!file_exists($new_file)){
                    //检查是否有该文件夹，如果没有就创建，并给予最高权限
                    mkdir($new_file, 0700);
                }
                $new_file = $new_file.time().".{$type}";
                if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64url)))){
                    $new = md5(time());
                    //压缩图片
                    image_png_size_add($new_file,ROOT_PATH . 'public' . DS . 'uploads/face/'.date('Ymd',time())."/".$new.".{$type}");
                    $url =  "http://".$_SERVER['HTTP_HOST'];
                    $file_name = $url."/uploads/face/".date('Ymd',time())."/".$new.".{$type}";
                    $param['portrait'] = $file_name;
                    $flag = $user->editUser($param);
                    //删除未压缩前图片
                    unlink($new_file);
                    $group_access = Db::name('auth_group_access')->where('uid', $user['id'])->update(['group_id' => $param['groupid']]);
                    //判断编辑的是不是自己的头像
                    if(session('uid')==$param['id']){
                        $have = "have";
                    }else{
                        $have = "";
                    }
                    return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg'],'type'=>$have]);
                }else{
                    writelog(session('uid'),session('username'),'编辑管理员【'.$param['username'].'】上传头像失败',2);
                    return json(['code'=>100,'msg'=>"上传失败"]);
                }
            }else{
                $flag = $user->editUser($param);
                $group_access = Db::name('auth_group_access')->where('uid', $user['id'])->update(['group_id' => $param['groupid']]);
                return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg'],'type'=>'']);
            }
        }

        $id = input('param.id');
        if($id != "1"){
            $role = new UserType();
            $this->assign([
                'user' => $user->getOneUser($id),
                'role' => $role->getRole()
            ]);
            //普通管理员编辑页面
            return $this->fetch("user/useredit");
        }else{
            $this->assign([
                'user' => $user->getOneUser($id)
            ]);
            //超级管理员编辑页面
            return $this->fetch("user/editadmin");
        }

    }


    /**
     * [adminEdit 编辑超级管理员]
     * @return [type] [description]
     * @author
     */
    public function adminEdit(){
        $user = new UserModel();
        $oldpassword = md5(md5(input('oldpassword')).config('auth_key'));
        if(input('type')=="checkPassword"){
            echo $user->checkOldPassword ($oldpassword,session('uid'));
        }else{
            $param = input('post.');
            if(empty($param['password'])){
                unset($param['password']);
            }else{
                $param['password'] = md5(md5($param['password']) . config('auth_key'));
            }
            $base64url = $param['portrait'];
            //匹配出图片的格式
            $base64url = str_replace(' ', '+', $base64url);
            if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64url, $result)){
                $type = $result[2];
                $new_file = ROOT_PATH . 'public' . DS . 'uploads/face/'.date('Ymd',time())."/";
                if(!file_exists($new_file)){
                    //检查是否有该文件夹，如果没有就创建，并给予最高权限
                    mkdir($new_file, 0700);
                }
                $new_file = $new_file.time().".{$type}";
                if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64url)))){
                    $new = md5(time());
                    //压缩图片
                    image_png_size_add($new_file,ROOT_PATH . 'public' . DS . 'uploads/face/'.date('Ymd',time())."/".$new.".{$type}");
                    $url =  "http://".$_SERVER['HTTP_HOST'];
                    $file_name = $url."/uploads/face/".date('Ymd',time())."/".$new.".{$type}";
                    $param['portrait'] = $file_name;
                    $flag = $user->editUser($param);
                    //删除未压缩前图片
                    unlink($new_file);
                    if(session('uid')==$param['id']){
                        $have = "have";
                    }else{
                        $have = "";
                    }
                    return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg'],'type'=>$have]);
                }else{
                    writelog(session('uid'),session('username'),'编辑超级管理员【'.$param['username'].'】上传头像失败',2);
                    return json(['code'=>100,'msg'=>"上传失败"]);
                }
            }else{
                $flag = $user->editUser($param);
                return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg'],'type'=>'']);
            }
        }
    }

    /**
     * [UserDel 删除用户]
     * @return [type] [description]
     * @author
     */
    public function UserDel()
    {
        $id = input('param.id');
        if(session('uid')==$id){
            return json(['code'=>100,'msg'=>'不能删除自己']);
        }else{
            $role = new UserModel();
            $flag = $role->delUser($id);
            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
        }
    }
//    /**
//     * [UserDel 删除用户]
//     * @return [type] [description]
//     * @author
//     */
//    public function UserDel()
//    {
//        extract(input());
//        $role = new UserModel();
//        if($type == 'single'){//单个删除
//            if(session('uid')==$id){
//                return json(['code'=>100,'msg'=>'不能删除自己']);
//            }else{
//                $flag = $role->delUser($id);
//                return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
//            }
//        }elseif($type == 'delAll'){//批量删除
//            if(empty($id)){
//                return json(['code'=>100,'msg'=>'请选择要删除的记录！']);
//            }
//            $ids = explode(',',$id);
//            if(in_array('1',$ids)){
//                $key = array_search ('1',$ids);
//                unset($ids[$key]);
//                if(empty($ids)){
//                    return json(['code'=>100,'msg'=>'不可删除超级管理员']);
//                    die;
//                }
//            }
//            if(in_array(session('uid'),$ids)){
//                $key = array_search (session('uid'),$ids);
//                unset($ids[$key]);
//                if(empty($ids)){
//                    return json(['code'=>100,'msg'=>'不可删除自己']);
//                    die;
//                }
//            }
//            $ids = array_merge($ids);
//            $flag = $role->batchDelUser($ids);
//            return json(['code' => $flag['code'], 'msg' => $flag['msg']]);
//        }
//
//    }



    /**
     * [user_state 用户状态]
     * @return [type] [description]
     * @author
     */
    public function user_state()
    {
        extract(input());
        $role = new UserModel();
        $flag = $role->userState($id,$num);
        return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
    }

    /**
     * theme 用户修改主题
     * @return \think\response\Json
     */
    public function theme(){
        $type =  input('id');
        $param['theme'] = $type;
        $role = new UserModel();
        $flag = $role->changeTheme($param);
        return json(['code' => $flag['code'],'msg' => $flag['msg']]);
    }

    /**
     * editPwd 修改管理员密码
     * @return \think\response\Json
     */
    public function editPwd(){
        extract(input());
        $user = new UserModel();
        if(isset($type) && $type=='checkPassword'){
            $old_pwd = md5(md5($old_pwd).config('auth_key'));
            echo $user->checkOldPassword ($old_pwd,session('uid'));
        }else{
            $param['password'] = md5(md5($new_pwd).config('auth_key'));
            $flag = $user->editPassword($param);
            return json(['code'=>$flag['code'],'msg'=>$flag['msg']]);
        }
    }

    /**
     * batchDelUser 批量删除管理员
     * @return \think\response\Json
     */
    public function batchDelUser(){
        extract(input());
        if(empty($ids)){
            return json(['code'=>100,'msg'=>'请选择要删除的记录！']);
        }
        $ids = explode(',',$ids);
        if(in_array('1',$ids)){
            $key = array_search ('1',$ids);
            unset($ids[$key]);
            if(empty($ids)){
                return json(['code'=>100,'msg'=>'不可删除超级管理员']);
                die;
            }
        }
        if(in_array(session('uid'),$ids)){
            $key = array_search (session('uid'),$ids);
            unset($ids[$key]);
            if(empty($ids)){
                return json(['code'=>100,'msg'=>'不可删除自己']);
                die;
            }
        }
        $ids = array_merge($ids);
        $user = new UserModel();
        $flag = $user->batchDelUser($ids);
        return json(['code' => $flag['code'], 'msg' => $flag['msg']]);
    }


    /**
     * usingAdmin 批量启用管理员
     * @return \think\response\Json
     */
    public function usingAdmin(){
        extract(input());
        $list = [];
        if($ids){
            $ids = explode(',',$ids);
            for($i=0;$i<count($ids);$i++){
                $param = [
                    'id'=>$ids[$i],
                    'status'=>1
                ];
                $list[] = $param;
            }
        }
        $user = new UserModel();
        $flag = $user->usingAdmin($list);
        return json(['code' => $flag['code'], 'msg' => $flag['msg']]);
    }

    /**
     * forbiddenAdmin 批量禁用管理员
     * @return \think\response\Json
     */
    public function forbiddenAdmin(){
        extract(input());
        $list = [];
        if($ids){
            $ids = explode(',',$ids);
            if(in_array('1',$ids)){
                $key = array_search ('1',$ids);
                unset($ids[$key]);
                if(empty($ids)){
                    return json(['code'=>100,'msg'=>'不可禁用超级管理员']);
                    die;
                }
            }
            if(in_array(session('uid'),$ids)){
                $key = array_search (session('uid'),$ids);
                unset($ids[$key]);
                if(empty($ids)){
                    return json(['code'=>100,'msg'=>'不可禁用自己']);
                    die;
                }
            }
            $ids = array_merge($ids);
            for($i=0;$i<count($ids);$i++){
                $param = [
                    'id'=>$ids[$i],
                    'status'=>2
                ];
                $list[] = $param;
            }
        }
        $user = new UserModel();
        $flag = $user->forbiddenAdmin($list);
        return json(['code' => $flag['code'], 'msg' => $flag['msg']]);
    }

    /**
     * 导出Excel
     * @return \think\response\Json
     */
    public function excelAdmin(){
        extract(input());
        if($ids =="" && $key == "" && $start == "" && $end == "" && $role ==""){
            $data = Db::name('admin')->select();
        }
        if($ids != ""){
            $ids = trim($ids,',');
            $ids = explode(',',$ids);
            $data = Db::name('admin')->where('id','in',$ids)->select();
        }else{
            $map = [];
            if($role != ""){
                $map['ag.id'] = $role;
            }
            if($key!="")
            {
                $map['a.username|a.real_name'] = ['like',"%" . $key . "%"];
            }
            if($start!=""&&$end=="")
            {
                $map['a.last_login_time'] = ['>= time',$start];
            }
            if($end!=""&&$start=="")
            {
                $map['a.last_login_time'] = ['<= time',$end];
            }
            if($start!=""&&$end!="")
            {
                $map['a.last_login_time'] = ['between time',[$start,$end]];
            }
            $data = Db::name('admin')
                ->alias ('a')
                ->join('auth_group ag', 'a.groupid = ag.id','left')
                ->where($map)
                ->select();
        }
        $cellname = [
            ['id','ID',15,'LEFT'],
            ['username','昵称',15,'LEFT'],
            ['password','密码',15,'LEFT'],
            ['portrait','头像',20,'LEFT'],
            ['loginnum','登录次数',15,'LEFT'],
            ['last_login_ip','上次登录ip',15,'LEFT'],
            ['last_login_time','上次登录时间',15,'LEFT'],
            ['real_name','真实姓名',15,'LEFT'],
            ['status','状态',15,'LEFT'],
            ['groupid','角色id',15,'LEFT'],
            ['theme','主题',15,'LEFT'],
            ['create_time','创建时间',15,'LEFT'],
            ['update_time','修改时间',15,'LEFT']
        ];
        $res = exportExcel('管理员信息','admin',$cellname,$data);
        return json($res);
    }

}